package com.adcc.sgs.gcs.service;

import com.adcc.sgs.gcs.db.DBAccess;
import com.adcc.sgs.gcs.entity.AreaUserEntity;
import com.adcc.sgs.gcs.entity.QueueEntity;
import com.adcc.sgs.gcs.entity.StationEntity;
import com.adcc.sgs.gcs.proxy.AdapterProxy;
import com.adcc.sgs.gcs.repository.AreaUserRepository;
import com.adcc.sgs.gcs.repository.QueueRepository;
import com.adcc.sgs.gcs.repository.StationRepository;
import com.google.common.base.Optional;
import com.smartUtility.common.log.Log;
import com.smartUtility.mq.transfer.TransferType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * Created by liuyi on 2016/8/17.
 */
public class SendQueueService {
    /*
    获取当前  MQ 实体
     */
    public QueueEntity findSendQueue(Integer stationId) {
        SqlSession session = null;
        try {
            Optional<DBAccess> dbAccess = AdapterProxy.getInstance().getDbAccess();
            if (dbAccess != null) {
                SqlSessionFactory sqlSessionFactory = dbAccess.get().getSessionFactory();
                session = sqlSessionFactory.openSession();
                //获取station
                StationRepository stationRepository = session.getMapper(StationRepository.class);
                StationEntity stationEntity = stationRepository.selectStation(stationId);
                if (stationEntity != null) {
                    //获取areauser
                    AreaUserRepository areaUserRepository = session.getMapper(AreaUserRepository.class);
                    AreaUserEntity areaUserEntity = areaUserRepository.selectAreaUser(stationEntity.getAreauser_id());
                    if (areaUserEntity != null) {
                        //获取queue
                        QueueRepository queueRepository = session.getMapper(QueueRepository.class);
                        QueueEntity queueEntity = queueRepository.selectQueue(areaUserEntity.getQueue_id());
                        session.close();
                        return queueEntity;
                    }
                }
            }
        } catch (Exception ex) {
            Log.error(getClass().getName(), "findSendQueueName() error", ex);
        }
        return null;
    }

    /*
    转发到当前MQ
     */
    public void sendMessageToDestination(QueueEntity sendQueueEntity, String recStr) {
        if (AdapterProxy.getInstance().getActivemqSendClient().isPresent()) {
            try {
                if (sendQueueEntity != null && sendQueueEntity.getName() != null && !sendQueueEntity.getName().equals("null")) {
                    AdapterProxy.getInstance().getActivemqSendClient().get().send(sendQueueEntity.getName(), recStr, TransferType.QUEUE);
                    Log.info(getClass().getName(), "send message to:" + sendQueueEntity.getName() + "\r\n" + recStr);
                } else {
                    throw new Exception("sendQueue is null");
                }
            } catch (Exception ex) {
                Log.error(getClass().getName(), "sendMessageToDestination() error", ex);
            }
        }
    }
}
